/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : MqcpDeviceInfoReport.c
  * @brief          : MqcpDeviceInfoReport
  ******************************************************************************
  * @attention
  * 本文件用于  用户信息上报
  * @20210414:文件模板第一次生成
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */


#include "MqcpTaskAdd.h"
#include "SystemConfigTask.h"
#include "SystemTimeListTask.h"

static MqcpDataDef *MqcpTaskAddReqProcess(MqcpMessageDef *Message);
static MqcpDataDef *MqcpTaskAddRespProcess(MqcpMessageDef *Message);


void TaskAddInit(void)//用户信息上报初始化
{
	MqcpRouterDef routeReq = 
	{
		1,
		TaskAddReq,
		MqcpTaskAddReqProcess
	};
	while(addRouter(routeReq));
	
	MqcpRouterDef routeResp = 
	{
		1,
		TaskAddResp,
		MqcpTaskAddRespProcess
	};
	while(addRouter(routeResp));
}

uint16_t TimeList_id = 0;

MqcpDataDef *MqcpTaskAddReqProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	sTimeListWorkDef *MqcpTimeList = (sTimeListWorkDef *)(&(MqcpDataType.Modbus_TIM[0]));
	
	MqcpData->isNeedResp = 1;
	MqcpData->Size = 0;
	
	for(uint16_t i = 0; i < MqcpTimeList->TimeListSizeMax; i++)
	{
		TimeList_id = i + 1;
		if(0 == MqcpTimeList->TimeList[i].Id)
		{
			GetTimeList(Message->Data, i, TimeListAdd);
			TimeListisWork[i] = 0;
			break;
		}
	}
	return MqcpData;
}

MqcpDataDef *MqcpTaskAddRespProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	sTimeListWorkDef *MqcpTimeList = (sTimeListWorkDef *)(&(MqcpDataType.Modbus_TIM[0]));
	
	MqcpData->isNeedResp = 0;
	MqcpData->Size = 0;
	
	uint16_t MqcpSize = 0;//发送的data数
	
	rt_memcpy(MqcpData->MqcpMessage.MsgId,Message->MsgId,12);MqcpData->Size = MqcpData->Size +12;
	MqcpData->MqcpMessage.Sync = MqcpSyncFalse;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Code = MqcpCodeOK;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Ver = MQCP_USE_VER;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.DevID[3] = MqcpDevice.DevId>>24;
	MqcpData->MqcpMessage.DevID[2] = MqcpDevice.DevId>>16;
	MqcpData->MqcpMessage.DevID[1] = MqcpDevice.DevId>>8;
	MqcpData->MqcpMessage.DevID[0] = MqcpDevice.DevId&0xff;MqcpData->Size = MqcpData->Size + 4;
	MqcpData->MqcpMessage.Cmd = TaskAddResp;MqcpData->Size = MqcpData->Size + 1;
	
	MqcpData->MqcpMessage.Data[MqcpSize] = ((uint16_t)(MqcpTimeList->TimeListSize))&0xff;MqcpData->Size = MqcpData->Size + 1;MqcpSize = MqcpSize + 1;
	MqcpData->MqcpMessage.Data[MqcpSize] = ((uint16_t)(MqcpTimeList->TimeListSizeMax))&0xff;MqcpData->Size = MqcpData->Size + 1;MqcpSize = MqcpSize + 1;
	
	MqcpData->MqcpMessage.Data[MqcpSize] = TimeList_id&0xff;MqcpData->Size = MqcpData->Size + 1;MqcpSize = MqcpSize + 1;
	
	rt_memcpy((&(MqcpData->MqcpMessage.Data[MqcpSize])),(&(Message->Data[1])),13);MqcpData->Size = MqcpData->Size + 13;MqcpSize = MqcpSize + 13;
	
	MqcpData->MqcpMessage.Len[1] = MqcpSize>>8;
	MqcpData->MqcpMessage.Len[0] = MqcpSize&0xff;MqcpData->Size = MqcpData->Size + 2;
	
	MqcpData->MqcpMessage.Data[MqcpSize] = Chack_Xor((uint8_t*)(&(MqcpData->MqcpMessage)),MqcpData->Size);MqcpData->Size = MqcpData->Size + 1;
	
	return MqcpData;
}

/************************ (C) COPYRIGHT CunJu *****END OF FILE****/
